using RAG.Domain.Services;

namespace RAG.Infrastructure.Services;

/// <summary>
/// 用于测试的假向量生成服务
/// </summary>
public class MockEmbeddingService : IEmbeddingService
{
    public async Task<float[]> GenerateTextEmbeddingAsync(string text)
    {
        // 生成一个1024维的随机向量用于测试
        var random = new Random(text.GetHashCode()); // 使用文本哈希作为种子，确保相同文本生成相同向量
        var embedding = new float[1024];

        for (int i = 0; i < 1024; i++)
        {
            embedding[i] = (float)(random.NextDouble() * 2 - 1); // 生成-1到1之间的随机数
        }

        // 标准化向量
        var magnitude = Math.Sqrt(embedding.Sum(x => x * x));
        if (magnitude > 0)
        {
            for (int i = 0; i < embedding.Length; i++)
            {
                embedding[i] = (float)(embedding[i] / magnitude);
            }
        }

        await Task.Delay(100); // 模拟API调用延迟

        return embedding;
    }

    public async Task<float[]> GenerateMultimodalEmbeddingAsync(byte[] imageData, string? text = null)
    {
        // 结合图片数据和文本生成向量
        var combinedInput = $"image_{imageData.Length}_{text ?? ""}";
        return await GenerateTextEmbeddingAsync(combinedInput);
    }
}
